package com.jcfk.eam.controller.bd;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.eam.domain.dto.base.EamLocationInfoDTO;
import com.jcfk.eam.domain.query.base.EamLocationInfoQuery;
import com.jcfk.eam.service.base.EamLocationInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 位置信息 服务接口
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-04-24
 */
@Api(tags = {"位置信息"})
@RestController
@RequestMapping("locationInfo")
public class EamLocationInfoController {
    @Autowired
    private EamLocationInfoService EamLocationInfoService;

    /**
     * 查询位置列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "EamLocationInfoQuery", name = "query", value = "", required = true)})
    @ApiOperation(value = "查询位置列表", notes = "查询位置列表", httpMethod = "POST")
    @PostMapping("getList")
    public RetResult<List<EamLocationInfoDTO>> getList(@RequestBody EamLocationInfoQuery query) {
        List<EamLocationInfoDTO> list = EamLocationInfoService.getList(query);
        return RetResult.ok(list);
    }


    /**
     * 获取位置树
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "String[]", name = "ids", value = "", required = true)})
    @ApiOperation(value = "获取位置树", notes = "获取位置树", httpMethod = "POST")
    @PostMapping("getTree")
    public RetResult<List<EamLocationInfoDTO>> getTree(@RequestBody EamLocationInfoQuery query) {
        return RetResult.ok(EamLocationInfoService.getTree(query));
    }


    /**
     * 获取位置信息
     * 根据id 查询详情
     */
    @ApiOperation("获取位置信息")
    @GetMapping("/get/{id}")
    public RetResult<EamLocationInfoDTO> get(@PathVariable("id") String id) {
        EamLocationInfoDTO item = EamLocationInfoService.get(id);
        return RetResult.ok(item);
    }


    @ApiOperation("保存位置信息")
    @PostMapping("/save")
    @RequestLogHeader(modular = "位置信息", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamLocationInfoDTO EamLocationInfo) {
        // 数据校验
        RetResult ret = EamLocationInfoService.isValid(EamLocationInfo);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }
        // 数据校验
        if (StrUtil.isEmpty(EamLocationInfo.getId())) {
            // 新增
            EamLocationInfoService.insert(EamLocationInfo);
        } else {
            // 更新
            EamLocationInfoService.update(EamLocationInfo);
        }
        return RetResult.ok();

    }

    /**
     * 批量删除位置信息
     */
    @ApiOperation("批量删除位置信息")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "位置信息", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamLocationInfoDTO dto) {
        // 校验
        for (String id : dto.getIds()) {
            Boolean haveChild = EamLocationInfoService.isHaveChild(id);
            if (haveChild.equals(true)) {
                return RetResult.fail("不能删除有下级的位置信息");
            }
        }
        return RetResult.ok(EamLocationInfoService.logicDeleteList(dto));
    }

}
